package com.elminster.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

    public static List<String> readLines(String filename) throws Exception {
        List<String> lines = new ArrayList<String>();
        FileInputStream in = null;
        BufferedReader reader = null;
        String line = null;
        try {
            in = new FileInputStream(filename);
            reader = new BufferedReader(new InputStreamReader(in));
            while (null != (line = reader.readLine())) {
                lines.add(line);
            }
        } finally {
            if (null != reader) {
                reader.close();
            }
            if (null != in) {
                in.close();
            }
        }
        return lines;
    }
    
    public static void createFolder(String filePath) throws IOException {
        String str = filePath.replace("\\", "/");
        int i = -1;
        String path = "";
        File file = null;
        while (-1 != (i = str.indexOf("/"))) {
            String parent = str.substring(0, i);
            str = str.substring(i + 1);
            path += parent + "/";
            file = new File(path);
            if (file.isDirectory()) {
                continue;
            } else {
                file.mkdir();
            }
        }
    }
}
